package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import com.itheima.service.ExprServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
     @Autowired
     private DeptService deptService;
     @Autowired
     private ExprServices exprServices;
    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {
        Page<Emp> page = new Page<>(param.getPage(),param.getPageSize());

        /*LambdaQueryWrapper<Emp> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(param.getName() != null && param.getName() != "",Emp::getName,param.getName())
                .eq(param.getGender() != null,Emp::getGender,param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate,param.getBegin(),param.getEnd());
*/
         page = lambdaQuery().like(param.getName() != null && param.getName() != "", Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd()).page(page);

        List<Emp> emps = page.getRecords();
        List<Integer> deptIds = emps.stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());
        if(!deptIds.isEmpty()){
            List<Dept> depts = deptService.listByIds(deptIds);
            emps.forEach( emp->{
                depts.forEach(dept ->{
                    if(dept != null && emp.getDeptId().equals(dept.getId())){
                        emp.setDeptName(dept.getName());
                    }
                });
            });
        }
        return new PageResult<>(page.getTotal(), emps);

    }

    @Override
    public void saveExpr(Emp emp) {
        //补全基础属性
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //保存员工信息
        save(emp);
        //保存员工工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if(exprList != null && !exprList.isEmpty()){
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
        }
        exprServices.saveBatch(exprList);
    }

    @Override
    public void deleteEmps(List<Integer> ids) {
        removeByIds(ids);
        exprServices.remove(Wrappers.<EmpExpr>lambdaQuery().in(EmpExpr::getEmpId,ids));
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);

        exprServices.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if(exprList != null && exprList.isEmpty()){
            exprList.forEach(expr ->{
                expr.setEmpId(emp.getId());
            });
            exprServices.saveBatch(exprList);
        }

    }

    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);
        emp.setExprList(exprServices.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,id)));
        return emp;
    }
}
